/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * OpenID entity data model.  Used for OpenID logins.
 * @author Adam
 * @version $Id: OpenID.java 30 2010-01-10 19:05:55Z a.ruggles $
 * 
 * Created on Apr 14, 2009 at 7:00:27 PM 
 */
@javax.persistence.Entity @Table(name = "sapien_openids")
public class OpenID implements Serializable {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 6145460423965158885L;

	/**
	 * Unique identifier.
	 */
	private String id;

	/**
	 * The user associated with the openid.
	 */
	private User user;

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof OpenID)) {
			return false;
		}
		final OpenID rhs = (OpenID) obj;
		return new EqualsBuilder()
			.append(id, rhs.getId())
			.isEquals();
	}

	/**
	 * Returns id.
	 * @return the id.
	 */
	@Id @Column(name = "openid_url")
	public String getId() {
		return id;
	}

	/**
	 * Returns user.
	 * @return the user.
	 */
	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(59, 37)
			.append(id)
			.toHashCode();
	}

	/**
	 * Sets id.
	 * @param id the id to set.
	 */
	public void setId(final String id) {
		this.id = id;
	}

	/**
	 * Sets user.
	 * @param user the user to set.
	 */
	public void setUser(final User user) {
		this.user = user;
	}

	/**
	 * {@inheritDoc}
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this)
			.append("id", id)
			.append("user", user)
			.toString();
	}
}
